package com.pureland.storage.bean;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * @projectName: pureland_cloud
 * @className: com.pureland.storage.bean.Storage
 * @description:
 * @author: tong.li
 * @createTime: 2020/10/19 14:29
 * @version: v1.0
 * @copyright: 版权所有 李彤 © 2020
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class Storage {

    private String originName;
    private String bucketName;
    @Builder.Default
    private boolean randomName = true;
    private PrefixRule rule;
    private String prefix;
    private String acl;


    public enum PrefixRule {
        /**
         * 无规则
         */
        NONE,
        /**
         * 当前日期+月份
         */
        NOW_DATE_MOUTH,

        /**
         * 当前年月日
         */
        NOW_DATE_MOUTH_DAY;
    }


    /**
     * 检查Storage变量,没有则获取一个默认的Storage变量
     * @param storage
     * @param originName
     * @param bucketName
     * @return
     */
    public static Storage checkDefaultStorage(Storage storage, String  originName,String bucketName) {
        // 如果不存在，设置一个随机名称(UUID)
        originName = StringUtils.isEmpty(originName) ? UUID.randomUUID().toString().replaceAll("-","") : originName;
        if (storage == null) {
            log.warn("If the object is empty, it will enter the default configuration");
            storage = Storage.builder()
                    .randomName(false)
                    .originName(originName)
                    .bucketName(bucketName)
                    .build();
        }
        return storage;
    }
    
}
